{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "5bc4f42b",
   "metadata": {},
   "source": [
    "* 本学习记录人：莫金婵\n",
    "* 学习时间：2021-11-18\n",
    "* week07\n",
    "* [参考文档](https://www.python.org/doc/)\n",
    "* [博客](https://www.cnblogs.com/xuzhichao)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "63f6fae2",
   "metadata": {},
   "source": [
    "# 回顾上周内容：\n",
    "\n",
    "## 1. 认识列表：[value1,value2,value3...]\n",
    "\n",
    "## 2. 列表的查询：\n",
    "\n",
    "> 1.索引值进行查询（单个值）：index\n",
    "> 2.切片进行查询（多值）：slice\n",
    "\n",
    "## 3.列表的一般方法\n",
    "\n",
    "> 1.增加元素\n",
    "\n",
    ">> 1.append() 增加元素到列表到末尾: L.append(object)    \n",
    ">> 2.insert() 指定位置增加元素 : L.insert(index)  \n",
    "\n",
    "> 2.删除元素\n",
    "\n",
    ">> 1.remove() 按照 值 进行删除 : L.remove(value)    \n",
    ">> 2.pop()按照 索引值 进行删除： L.pop(index) 注意：不写index 默认删除列表的最后一个元素\n",
    "\n",
    "> 3.修改内容\n",
    "\n",
    "> 4.查询内容（参考上述2）\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "48906af2",
   "metadata": {},
   "source": [
    "# 2.本周内容：\n",
    "\n",
    "## 1.列表的其他用法\n",
    "\n",
    "## 2.列表的课本实践\n",
    "\n",
    "## 3.列表的课外实践"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "d723d1aa",
   "metadata": {},
   "outputs": [],
   "source": [
    "names = [\"mike\",\"kiki\",\"jack\",\"lcy\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "ff119c0d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['mike', 'kiki', 'jack', 'lcy']"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e425f71c",
   "metadata": {},
   "source": [
    "> 1.clear()\n",
    ">> A.使用方法：L.clear()\n",
    "\n",
    ">> B.结果：removeall items form L\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "532ae291",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function clear:\n",
      "\n",
      "clear() method of builtins.list instance\n",
      "    Remove all items from list.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(names.clear)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "485b661e",
   "metadata": {},
   "outputs": [],
   "source": [
    "names.clear()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "fd47d1d9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[]"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f4ae6ac8",
   "metadata": {},
   "source": [
    "> 2.count()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "bad88174",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function count:\n",
      "\n",
      "count(value, /) method of builtins.list instance\n",
      "    Return number of occurrences of value.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(分数_list.count)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "56d58e50",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[88, 89, 84, 86, 68, 93, 88]"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "分数_list = [88,89,84,86,68,93,88]\n",
    "分数_list"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "459adab6",
   "metadata": {},
   "source": [
    "> 3.extend()拓展\n",
    ">> 1,使用方法：L.estend(iterable)    \n",
    ">> 2,结果: iterable 将拼接在 L 的后面（末尾）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "628d3751",
   "metadata": {},
   "outputs": [],
   "source": [
    "分数_list_数技一班 = [88,89,84,86,68,93,88]\n",
    "分数_list_数技四班 = [88,77,75,87,95]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "b3c2fe3d",
   "metadata": {},
   "outputs": [],
   "source": [
    "分数_list_数技一班.extend(分数_list_数技四班)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "43aa63b7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[88, 89, 84, 86, 68, 93, 88, 88, 89, 84, 86, 68, 93, 88, 88, 77, 75, 87, 95]"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "分数_list_数技一班"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "dac7d518",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[88, 89, 84, 86, 68, 93, 88, 88, 77, 75, 87, 95]"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#等价于\n",
    "[88,89,84,86,68,93,88] + [88,77,75,87,95]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f61217dd",
   "metadata": {},
   "source": [
    "> 4.<font style=\"color:red\"> **copy**</font>\n",
    "\n",
    ">> 1. 在列表中赋值不等价于复制    \n",
    ">> 2. 真正的复制列表的两种常见方法：    \n",
    ">>copy()    \n",
    ">>切片[:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8991f340",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "44f5210c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "jack alex\n",
      "2724733399856\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "(None, 2724733430832)"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name1 = \"alex\"\n",
    "name2 = name1\n",
    "name1 =\"jack\"\n",
    "print(name1,name2)\n",
    "print(id(name1)) , (id(name2))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6cfab2fa",
   "metadata": {},
   "source": [
    "* 结论是：赋值有两种情况：\n",
    "\n",
    "> 1.赋值的数据类型是 不可变的数据 （字符串、数值、元组），分配不同的数据地址\n",
    "> 2.赋值的数据类型是 可变的数数据 （列表） 分配同一个地址，指向同一个列表\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "671356ec",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 此案例来源：课本p72：看起来像复制，但不是\n",
    "first = [1,2,3,4,5]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "7166ad95",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, 4, 5]"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "first"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "79d64f16",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2724732230528\n"
     ]
    }
   ],
   "source": [
    "print(id(first))# first的数据地址 ：2724732230528"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "0593fa80",
   "metadata": {},
   "outputs": [],
   "source": [
    "second = first"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "37c668b6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, 4, 5]"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "second"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "ed986073",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2724732230528\n"
     ]
    }
   ],
   "source": [
    "print(id(second))#与first一样"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "4039af44",
   "metadata": {},
   "outputs": [],
   "source": [
    "first.append(6)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "072d1f05",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4, 5, 6] [1, 2, 3, 4, 5, 6]\n"
     ]
    }
   ],
   "source": [
    "print(first,second)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "009493c7",
   "metadata": {},
   "source": [
    "# 列表的复制（方法一）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "id": "b5ec8e71",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4, 5] [1, 2, 3, 4, 5]\n",
      "2724731683712 2724731683328\n"
     ]
    }
   ],
   "source": [
    "first = [1,2,3,4,5]\n",
    "second = first.copy()\n",
    "\n",
    "print(first,second)\n",
    "print(id(first),id(second))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "id": "48f82313",
   "metadata": {},
   "outputs": [],
   "source": [
    "first.append(6)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "id": "3290db35",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4, 5, 6] [1, 2, 3, 4, 5]\n"
     ]
    }
   ],
   "source": [
    "print(first,second)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d29f675d",
   "metadata": {},
   "source": [
    "# 列表的复制（方法二）\n",
    "* 切片 [:] 可以实现真正意义的复制"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "id": "e3e4982c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4, 5] [1, 2, 3, 4, 5]\n",
      "2724731686400 2724731686016\n"
     ]
    }
   ],
   "source": [
    "first = [1,2,3,4,5]\n",
    "second = first[:] \n",
    "\n",
    "print(first,second)\n",
    "print(id(first),id(second))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9ffc73b9",
   "metadata": {},
   "source": [
    "# 思考 copy（）只复制第一层"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "id": "84cb6b4c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 6, [3, 4, 5]] [1, 2, 6, [3, 4, 5]]\n",
      "2724733530240\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "(None, 2724733542592)"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "first = [1,2,6,[3,4,5]]\n",
    "second = first.copy()\n",
    "\n",
    "print(first,second)\n",
    "print(id(first)),id(second)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "id": "da15db1d",
   "metadata": {},
   "outputs": [],
   "source": [
    "first.append(6)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "id": "d270b7c5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 6, [3, 4, 5], 6] [1, 2, 6, [3, 4, 5]]\n"
     ]
    }
   ],
   "source": [
    "print(first,second)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "id": "d185f476",
   "metadata": {},
   "outputs": [
    {
     "ename": "AttributeError",
     "evalue": "'int' object has no attribute 'append'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-75-bb39378db26d>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mfirst\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m3\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mappend\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m7\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mAttributeError\u001b[0m: 'int' object has no attribute 'append'"
     ]
    }
   ],
   "source": [
    "first[3].append(7)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "id": "f930c43c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, 4, 5]"
      ]
     },
     "execution_count": 76,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "first"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e0c311b4",
   "metadata": {},
   "source": [
    "# 列表的实践（课本）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "id": "d2ec1178",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['D', 'o', 'n', \"'\", 't', ' ', 'p', 'a', 'n', 'i', 'c']\n",
      "['o', 'n', \"'\", 't', ' ', 'p', 'a', 'n', 'i', 'c']\n",
      "['o', 'n', \"'\", 't', ' ', 'p']\n",
      "['o', 'n', 't', ' ', 'p']\n",
      "['o', 'n', 't', ' ', 'p']\n",
      "['o', 'n', ' ', 't', 'p']\n"
     ]
    },
    {
     "ename": "NameError",
     "evalue": "name 'nwe_phrase' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-74-8a5991d2432b>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m     24\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mplist\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     25\u001b[0m \u001b[0mnew_phrase\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;34m''\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mjoin\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mplist\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 26\u001b[1;33m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mnwe_phrase\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     27\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     28\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mNameError\u001b[0m: name 'nwe_phrase' is not defined"
     ]
    }
   ],
   "source": [
    "# 目标：\"Don't panic\" => \"on tap\"\n",
    "# 利用列表的 方法 实现\n",
    "phrase = \"Don't panic\"\n",
    "\n",
    "plist = list(phrase)\n",
    "print(plist)\n",
    "# 删除D\n",
    "plist.remove(\"D\")\n",
    "print(plist)\n",
    "\n",
    "#删除末尾4个元素 用pop()删除四次\n",
    "for i in range(4):\n",
    "    plist.pop()\n",
    "print(plist)\n",
    "# 删除 ‘’\n",
    "plist.pop(2)\n",
    "print(plist)\n",
    "\n",
    "# plist.extend([plist.pop(),plist.pop()])# 难点在于先运算圆括号 再运算方括号\n",
    "plist.insert(4,plist.pop())\n",
    "print(plist)\n",
    "plist.insert(2,plist.pop(3))\n",
    "#['o','n','t','a','p'] 在第二位置加 ''\n",
    "print(plist)\n",
    "new_phrase = ''.join(plist)\n",
    "print(nwe_phrase)\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "id": "b8f624cb",
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'pop' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-70-681d5c018212>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mpop\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"D\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      2\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mplist\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mNameError\u001b[0m: name 'pop' is not defined"
     ]
    }
   ],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "13da67ee",
   "metadata": {},
   "source": [
    "# 列表的实践（课本）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "id": "b2f0c890",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['D', 'o', 'n', \"'\", 't', ' ', 'p', 'a', 'n', 'i', 'c', '!']\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "['o', 'n']"
      ]
     },
     "execution_count": 79,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 目标：\"Don't panic\" => \"on tap\"\n",
    "# 利用列表的 方法 实现\n",
    "phrase = \"Don't panic!\"\n",
    "\n",
    "plist = list(phrase)# list():str --> list\n",
    "print(plist)\n",
    "\n",
    "new_pht"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "id": "3098d23a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "' '"
      ]
     },
     "execution_count": 80,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "plist[5]+plist[]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "id": "349b37d0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'t'"
      ]
     },
     "execution_count": 81,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "plist[4]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7c5346be",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
